package com.example.tao.smartcar.wifi;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tao.smartcar.BaseActivity;
import com.example.tao.smartcar.R;
import com.example.tao.smartcar.SmartCarApplication;
import com.example.tao.smartcar.wifi.LAN.WifiTool;
import com.example.tao.smartcar.wifi.socket.SocketClient;

import java.io.IOException;
import java.util.List;

public class WifiModeChooseActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private Button searchBtn;
    private Button directConnectBtn;
    private Button throughRouterBtn;
    private Button remoteConnectBtn;
    private Button stopConnectBtn;
    private ListView deviceListview;
    private TextView mReadData;

    private WifiTool mWifitool;
    private SocketClient mSocketClient;
    private ArrayAdapter<String> mDeviceArrayAdapter;
    private String mSsid = null;

    private int mConnectMode;
    private String mDeviceApIp;
    private int mDeviceApPort = 333;
    private String mDeviceStaIp;
    private int mDeviceStaPort;
    private String mIp;
    private int mPort;

    public static int DIRECT_CONNECT = 0x01;
    public static int THROUGH_ROUTER_CONNECT = 0x02;
    public static int REMOTE_CONNECT = 0x03;


    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == SocketClient.MSG_REV)
                mReadData.setText(msg.getData().getString("data"));
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_mode_choose);
        SmartCarApplication.getInstance().addActivity(this);
        setToolbar("", false);

        initView();

        mWifitool = new WifiTool(this.getApplication());
        mSocketClient = new SocketClient();
        mSocketClient.setHandler(mHandler);
    }

    private void initView() {
        searchBtn = (Button) this.findViewById(R.id.search_btn);
        directConnectBtn = (Button) this.findViewById(R.id.direct_connect_btn);
        throughRouterBtn = (Button) this.findViewById(R.id.through_router_btn);
        remoteConnectBtn = (Button) this.findViewById(R.id.remote_connect_btn);
        stopConnectBtn = (Button) this.findViewById(R.id.stop_connect_btn);
        deviceListview = (ListView) this.findViewById(R.id.device);
        mReadData = (TextView) this.findViewById(R.id.read_data);

        searchBtn.setOnClickListener(this);
        directConnectBtn.setOnClickListener(this);
        throughRouterBtn.setOnClickListener(this);
        remoteConnectBtn.setOnClickListener(this);
        stopConnectBtn.setOnClickListener(this);

        mDeviceArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name_item);
        deviceListview.setAdapter(mDeviceArrayAdapter);
        deviceListview.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_btn :
                searchDevice();
                break;
            case R.id.direct_connect_btn :
                /*SocketClient socketClient = new SocketClient();
                socketClient.setIp("10.0.255.231");
                socketClient.setPort(10001);
                socketClient.send("guardian-2.4g");
                socketClient.receive();
                socketClient.setHandler(mHandler);*/
                directConnectDevice();
                break;
            case R.id.through_router_btn :

                break;
            case R.id.remote_connect_btn :

                break;
            case R.id.stop_connect_btn :
                mSocketClient.stopHeartBeat();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        view.setSelected(true);
        //mSsid = adapterView.getItemAtPosition(i).toString();
        mSsid = ((TextView)view).getText().toString();
    }

    private void searchDevice() {
        setToolbar("正在搜索设备...", false);
        mSsid = null;
        mDeviceArrayAdapter.clear();
        mWifitool.openWifi();
        mWifitool.scanWifi();
        List<String> wifiSsidList = mWifitool.getWifiSsidList();
        if ((wifiSsidList != null) && (wifiSsidList.size() > 0)) {
            for (int i = 0; i < wifiSsidList.size(); i++) {
                String ssid = wifiSsidList.get(i);
                if (ssid.length() > 0)
                    mDeviceArrayAdapter.add(ssid);
            }
            setToolbar("请选择要连接的设备", false);
        } else {
            setToolbar("未搜索到设备", false);
        }
    }

    private void directConnectDevice() {
        if (mSsid != null) {
            boolean connect = mWifitool.connectWifi(mSsid, "");
            //connect = true;
            if (connect) {
                mConnectMode = DIRECT_CONNECT;
                mDeviceApIp = mWifitool.getAPAddress();
                mDeviceApPort = 333;
                //mDeviceApIp = "10.0.255.231";
                //mDeviceApPort = 10002;
                mSocketClient.setIp(mDeviceApIp);
                mSocketClient.setPort(mDeviceApPort);

                mSocketClient.startHeartBeat();

                Message message = new Message();
                message.what = SocketClient.MSG_SEND;
                Bundle bundle = new Bundle();
                bundle.putString(SocketClient.DATA, "guardian\n");
                message.setData(bundle);
                mSocketClient.send();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Handler handler = mSocketClient.getSendHandler();
                if (handler != null)
                    handler.sendMessage(message);

                mSocketClient.receive();
            } else {

            }
        }

    }
}
